package com.vader.juc.sysc;

/**
 * @author kaivader
 */
class Ticket {

    public int ticket = 30;

    public synchronized void sale() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出一张票，剩余：" + --ticket);
        }
    }

}
public class SyscSaleTicket{
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){
                    ticket.sale();
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){
                    ticket.sale();
                }
            }
        },"B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<40;i++){
                    ticket.sale();
                }
            }
        },"C").start();
    }
}